package org.yn.shopping.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yn.base.service.impl.BaseServiceImpl;
import org.yn.exception.AssertUtil;
import org.yn.exception.ResponseCode;
import org.yn.shopping.domain.ShoppingCart;
import org.yn.shopping.dto.ShoppingCartDTO;
import org.yn.shopping.dto.StateDTO;
import org.yn.shopping.mapper.ShoppingCartMapper;
import org.yn.shopping.service.IShoppingCartService;
import org.yn.shopping.vo.ShoppingCartVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yn
 * @since 2023-08-30
 */
@Service
public class ShoppingCartServiceImpl extends BaseServiceImpl<ShoppingCart> implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 添加购物车
     * @param dto
     */
    @Transactional
    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
   /*     添加购物车：
        前端点击 添加到购物车按钮，
        先判断数据库中是否已经有这个商品 有则直接数量加1，无则新增
        1.将商品的id 添加进去，
        2.该用户id也添加进去
         3.state修改为1 为选中状态*/

        // 判空
        AssertUtil.isNotBlank(String.valueOf(dto.getCarId()), ResponseCode.RESPONSE_CODE_500);
        AssertUtil.isNotBlank(String.valueOf(dto.getUserId()), ResponseCode.RESPONSE_CODE_500);

        //根据商品id查询是否存在这个商品
    ShoppingCart shoppingCart= shoppingCartMapper.selectByCarId(dto.getCarId());
    //判断是否为空
        if (Objects.nonNull(shoppingCart)){
            //不为空 说明之前已经添加过了  直接添加数量即可
            Integer quantity = shoppingCart.getQuantity()+1;
            shoppingCart.setQuantity(quantity);
            shoppingCartMapper.update(shoppingCart);//更新
        }else {
            // 为空说明需要插入
            ShoppingCart shoppingCartNew = new ShoppingCart();
            shoppingCartNew.setUserId(dto.getUserId());
            shoppingCartNew.setCarId(dto.getCarId());
            shoppingCartNew.setState(1);
            shoppingCartNew.setQuantity(1);
            shoppingCartMapper.insert(shoppingCartNew);
        }

    }

    @Override
    public List<List<ShoppingCartVO>> getShoppingCartByUserId(Long userId) {
        List<ShoppingCartVO> shoppingCartVOList =  shoppingCartMapper.getShoppingCartByUserId(userId);

        //  将店名一样的分到一个list里

        List<List<ShoppingCartVO>> groupedLists = shoppingCartVOList.stream()
                .filter(cart -> cart.getShopName() != null)
                .collect(Collectors.groupingBy(ShoppingCartVO::getShopName))
                .values().stream()
                .collect(Collectors.toList());

        System.out.println(groupedLists);

        return groupedLists;
    }

    @Override
    public void deleteByCarId(Long carId) {
        shoppingCartMapper.deleteByCarId(carId);
    }

    @Override
    public void deleteAllByUserId(Long userId) {
        shoppingCartMapper.deleteAllByUserId(userId);
    }

    @Override
    public void chageState(StateDTO dto) {
        //根据userid获取
      ShoppingCart shoppingCart=  shoppingCartMapper.selectByCarId(dto.getCarId());

      shoppingCart.setState(dto.getState());

      shoppingCartMapper.update(shoppingCart);


    }

    @Override
    public Integer count(Long userId) {
       Integer count= shoppingCartMapper.countByUserId(userId);

        return count;
    }

}
